/***************************************************************************
 $ @Description: 
 $ @Author: NavyPeng
 $ @Date: 2024-04-10 18:25:51
 $ @LastEditTime: 2024-04-19 15:13:18
 $ @LastEditors: NavyPeng
 ***************************************************************************/
#include "Ctrl.h"

#if StateModel_

#include <memory>

class Work;
class ForenoonState;
class NoonState;
class AfternoonState;
class EveningState;
class RestState;


//抽象状态
class State
{
public:
	virtual void WriteProgram(Work* ptrWork) = 0;
};



class Work
{
private:
	shared_ptr<State> smartState;
	double hour;
	bool finish;

public:
	Work();

	double GetHour() const {
		return hour;
	}
	void SetHour(const double _hour) {
		hour = _hour;
	}

	bool GetTaskFinished() const {
		return finish;
	}
	void SetTaskFinished(const bool _finish) {
		finish = _finish;
	}

	void SetState(shared_ptr<State> pState)
	{
		smartState = pState;
	}

	shared_ptr<State> GetState() const
	{
		return smartState;
	}

	void WriteProgram() {
		smartState->WriteProgram(this);
	}


};

class SleepingState : public State
{
public:
	void WriteProgram(Work* ptrWork) override
	{
		cout << "当前时间：" << ptrWork->GetHour() << "点 不行了，睡着了。" << endl;
	}
};

class RestState : public State
{
public:
	void WriteProgram(Work* ptrWork) override
	{
		cout << "当前时间：" << ptrWork->GetHour() << "点 下班回家了" << endl;
	}
};

class EveningState : public State
{
public:
	void WriteProgram(Work* ptrWork) override
	{
		if (ptrWork->GetTaskFinished()) {
			ptrWork->SetState(make_shared<RestState>());
			ptrWork->WriteProgram();
			return;
		}

		if (ptrWork->GetHour() < 21) {
			cout << "当前时间：" << ptrWork->GetHour() << "点 加班喔，疲累至极" << endl;
		}
		else {
			ptrWork->SetState(make_shared<SleepingState>());
			ptrWork->WriteProgram();
		}
	}
};

class AfternoonState : public State
{
public:
	void WriteProgram(Work* ptrWork) override
	{
		if (ptrWork->GetHour() < 17) {
			cout << "当前时间：" << ptrWork->GetHour() << "点 下午状态还不错，继续努力" << endl;
		}
		else {
			ptrWork->SetState(make_shared<EveningState>());
			ptrWork->WriteProgram();
		}
	}
}; 

class NoonState : public State
{
public:
	void WriteProgram(Work* ptrWork) override
	{
		if (ptrWork->GetHour() < 13) {
			cout << "当前时间：" << ptrWork->GetHour() << "点 饿了，午饭；犯困，午休。" << endl;
		}
		else {
			ptrWork->SetState(make_shared<AfternoonState>());
			ptrWork->WriteProgram();
		}
	}
};

class ForenoonState : public State
{
public:
	void WriteProgram(Work* ptrWork) override
	{
		if (ptrWork->GetHour() < 12) {
			cout << "当前时间：" << ptrWork->GetHour() << "点 上午工作，精神百倍" << endl;
		}
		else {
			ptrWork->SetState(make_shared<NoonState>());
			ptrWork->WriteProgram();
		}
	}
};


 


Work::Work() : hour(0), finish(false), smartState(std::make_shared<ForenoonState>()) {}

int main() {
	shared_ptr<Work> work = make_shared<Work>();
	work->SetHour(9);
	work->WriteProgram();
	work->SetHour(10);
	work->WriteProgram();
	work->SetHour(12);
	work->WriteProgram();
	work->SetHour(13);
	work->WriteProgram();
	work->SetHour(14);
	work->WriteProgram();
	work->SetHour(17);
	work->WriteProgram();

	work->SetTaskFinished(false);
	work->SetHour(19);
	work->WriteProgram();
	work->SetHour(22);
	work->WriteProgram();


}

#endif 
